package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.query.BpmnInfoQueryObject;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.util.Convert;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.github.pagehelper.PageHelper;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.Model;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Date;
import java.util.List;


@Service
@Transactional
public class BmbInfoServiceImpl implements IBpmnInfoService {

    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;

    @Autowired
    private RepositoryService repositoryService;


    @Override
    public TablePageInfo<BpmnInfo> query(BpmnInfoQueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<BpmnInfo>(bpmnInfoMapper.selectForList(qo));
    }


    @Override
    public void save(BpmnInfo bpmnInfo) {
        bpmnInfoMapper.insert(bpmnInfo);
    }

    @Override
    public BpmnInfo get(Long id) {
        return bpmnInfoMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(BpmnInfo bpmnInfo) {
        bpmnInfoMapper.updateByPrimaryKey(bpmnInfo);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            bpmnInfoMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<BpmnInfo> list() {
        return bpmnInfoMapper.selectAll();
    }

    @Override
    public void deploy(String bpmnInfoPath, String bpmnType, String info) throws FileNotFoundException {
        Deployment deploy = repositoryService.createDeployment().
                addInputStream(bpmnInfoPath, new FileInputStream(new File(SystemConfig.getProfile(), bpmnInfoPath)))
                .deploy();
        BpmnInfo bpmnInfo = new BpmnInfo();
        bpmnInfo.setBpmnType(bpmnType); // 设置类型
        bpmnInfo.setDeployTime(new Date());// 创建时间
        bpmnInfo.setDeploymentId(deploy.getId()); // 部署ID
        bpmnInfo.setInfo(info); // 备注
        // 获取流程定义信息，根据流程部署id获取
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
        bpmnInfo.setActProcessId(definition.getId());// 设置流程定义id
        bpmnInfo.setActProcessKey(definition.getKey());// 设置key
        bpmnInfo.setBpmnName(definition.getName());// 设置部署名称
        bpmnInfoMapper.insert(bpmnInfo);
    }


    @Override
    public InputStream readResource(String deploymentId, String type) {
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
        if ("xml".equalsIgnoreCase(type)) {
            // 获取保存的xml文件
            InputStream resourceAsStream = repositoryService.getResourceAsStream(deploymentId, definition.getResourceName());
            return resourceAsStream;
        } else {
            // 获取流程定义的模型
            BpmnModel model = repositoryService.getBpmnModel(definition.getId());
            // 获取插件绘图的对象
            ProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
            // 调用该方法生成图
            InputStream inputStream = generator.generateDiagram(model, Collections.EMPTY_LIST, Collections.EMPTY_LIST,
                    "宋体", "宋体", "宋体");
            return inputStream;
        }

    }

    @Override
    public void delete(Long id) {
        // 删除BpmnInfo
        // 获取到旧的
        BpmnInfo bpmnInfo = this.get(id);
        bpmnInfoMapper.deleteByPrimaryKey(id);

        // 删除流程定义
        ProcessDefinition definition = repositoryService.createProcessDefinitionQuery().deploymentId(bpmnInfo.getDeploymentId()).singleResult();
        repositoryService.deleteDeployment(bpmnInfo.getDeploymentId(),true);
        // 删除资源文件
        File file  = new File(SystemConfig.getProfile(),definition.getResourceName());
        file.delete();

    }

    @Override
    public BpmnInfo getBpmnByType(String type) {
        return bpmnInfoMapper.getBpmnByType(type);
    }
}
